// WndGSensor.cpp : implementation file
//

#include "stdafx.h"
#include "BBViewer.h"
#include "WndGSensor.h"

#include <math.h>

#include "StatusMgr.h"

enum 
{
	GSen_title = 0,
	GSen_Front,
	GSen_Rear,
	GSen_Left,
	GSen_Right,
	GSen_Up,
	GSen_Down
};

// CWndGSensor

CWndGSensor* g_pWndGSensor = NULL;
IMPLEMENT_DYNAMIC(CWndGSensor, CWnd)

CWndGSensor::CWndGSensor()
{	
}

CWndGSensor::~CWndGSensor()
{
	g_pWndGSensor = NULL;

	m_penEdge.DeleteObject();
	m_burshBoard.DeleteObject();
	if (m_dcBmpPanel.m_hDC != NULL)
		m_dcBmpPanel.DeleteDC();
	if (m_bmpPanel.m_hObject != NULL)
		m_bmpPanel.DeleteObject();

}


BEGIN_MESSAGE_MAP(CWndGSensor, CWnd)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_SIZE()
END_MESSAGE_MAP()



// CWndGSensor message handlers


int CWndGSensor::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	LPST_WND_POS lpWndPos = theApp.WndPos.GetAt(wpGSensorView);
	g_pWndGSensor = this;

	m_fSensorX	= 0;
	m_fSensorY  = 0;
	m_fSensorZ	= 0;

	int i, n;
	for (n=0;n<GSEN_ANGLE;n++)
	{
		for (i=0;i<GSEN_DIRECTION;i++)
		{
			m_bSensorOn[n][i] = FALSE;
			m_dwSetTime2[n][i] = 0;
		}
	}

	m_penEdge.CreatePen(PS_SOLID, 1, lpWndPos->dwColorLevel[0]);
	m_burshBoard.CreateSolidBrush(lpWndPos->dwColorBackground);
	m_dcBmpPanel.m_hDC = NULL;
	m_bmpPanel.m_hObject = NULL;

	LoadText();

	return 0;
}

void CWndGSensor::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// Do not call CWnd::OnPaint() for painting messages
	UpdateGSensor();

}

void CWndGSensor::InitDraw(int cx, int cy)
{
	LPST_WND_POS lpWndPos = theApp.WndPos.GetAt(wpGSensorView);
	if (lpWndPos->nMode == 0)
		InitDraw_m0(cx, cy);
	else if (lpWndPos->nMode == 1)
		InitDraw_m1(cx, cy);
}

void CWndGSensor::InitDraw_m0(int cx, int cy)
{
	CDC *pDC = GetDC();
	CDC dcBmp;
	CBitmap bmp, *pOldBmp;
	CPen *pOldPen;
	CBrush *pOldBrush;

	CDC dcBmpPanel;
	CBitmap bmpPanel, *pOldBmpPanel;
	CRect rtBmpPanel;
	CRect rect = CRect(0, 0, cx, cy);

	if (m_dcBmpPanel.m_hDC != NULL)
		m_dcBmpPanel.DeleteDC();

	if (m_bmpPanel.m_hObject != NULL)
		m_bmpPanel.DeleteObject();
	m_bmpPanel.CreateCompatibleBitmap(pDC, cx, cy);
	m_dcBmpPanel.CreateCompatibleDC(pDC);
	m_dcBmpPanel.SelectObject(&m_bmpPanel);

	LPST_WND_POS lpWndPos, lpWndPosCoordi;
	CRect rtDiv, rtImg;
	lpWndPos = theApp.WndPos.GetAt(wpGSensorView);
	lpWndPosCoordi = theApp.WndPos.GetAt(wpGSensorCoordi);
	rtDiv = lpWndPosCoordi->rtDiv;

	rtBmpPanel.left = 0;
	rtBmpPanel.right = cx-(lpWndPos->rtStickTo.left+lpWndPos->rtStickTo.right);
	rtBmpPanel.top = 0;
	rtBmpPanel.bottom = cy-(lpWndPos->rtStickTo.top+lpWndPos->rtStickTo.bottom);

	dcBmpPanel.CreateCompatibleDC(pDC);		//(1) create
	bmpPanel.CreateCompatibleBitmap(pDC, rtBmpPanel.Width(), rtBmpPanel.Height());	///(2) create
	pOldBmpPanel = dcBmpPanel.SelectObject(&bmpPanel);

	pOldPen = dcBmpPanel.SelectObject(&m_penEdge);
	pOldBrush = dcBmpPanel.SelectObject(&m_burshBoard);

	dcBmpPanel.Rectangle(&rtBmpPanel);

	dcBmpPanel.SelectObject(pOldBrush);
	dcBmpPanel.SelectObject(pOldPen);

	CBitmap *pBmp;
	LPST_BMP_INFO lpBmpInfo;

	lpBmpInfo = theApp.BmpInfo.GetAt(biGSenBG);
	if (lpBmpInfo->hbmpImage)
	{
		CDC dcBg;
		CBitmap *pBmpBg, *pOldBmpBg;

		dcBg.CreateCompatibleDC(pDC);
		pBmpBg = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
		pOldBmpBg = dcBg.SelectObject(pBmpBg);

		rtDiv = lpWndPos->rtDiv;
		rtImg = lpBmpInfo->rtImageRect;

		m_dcBmpPanel.BitBlt(0, 0, rtDiv.left, rtDiv.top, &dcBg, 0, 0, SRCCOPY);
		m_dcBmpPanel.StretchBlt(rtDiv.left, 0, rect.Width()-(rtDiv.left+rtDiv.right), rtDiv.top,
			&dcBg, rtDiv.left, 0, rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.top, SRCCOPY);
		m_dcBmpPanel.BitBlt(rect.Width()-rtDiv.right, 0, rtDiv.right, rtDiv.top,
			&dcBg, rtImg.Width()-rtDiv.right, 0, SRCCOPY);
		m_dcBmpPanel.StretchBlt(0, rtDiv.top, rtDiv.left, rect.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBg, 0, rtDiv.top, rtDiv.left, rtImg.Height()-(rtDiv.top+rtDiv.bottom), SRCCOPY);
		m_dcBmpPanel.StretchBlt(rect.Width()-rtDiv.right, rtDiv.top, rtDiv.right, rect.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBg, rtImg.Width()-rtDiv.right, rtDiv.top,
			rtDiv.right, rtImg.Height()-(rtDiv.top+rtDiv.bottom),
			SRCCOPY);
		m_dcBmpPanel.BitBlt(0, rect.Height()-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
			&dcBg, 0, rtImg.Height()-rtDiv.bottom, SRCCOPY);
		m_dcBmpPanel.StretchBlt(rtDiv.left, rect.Height()-rtDiv.bottom,
			rect.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
			&dcBg, rtDiv.left, rtImg.Height()-rtDiv.bottom,
			rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
			SRCCOPY);
		m_dcBmpPanel.BitBlt(rect.Width()-rtDiv.right, rect.Height()-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
			&dcBg, rtImg.Width()-rtDiv.right, rtImg.Height()-rtDiv.bottom, SRCCOPY);

		dcBg.SelectObject(pOldBmpBg);
		dcBg.DeleteDC();
	}


	lpBmpInfo = theApp.BmpInfo.GetAt(biGSenCoordi);
	pBmp = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
	dcBmp.CreateCompatibleDC(pDC);
	pOldBmp = dcBmp.SelectObject(pBmp);

	rtDiv = lpWndPosCoordi->rtDiv;
	rtImg = lpBmpInfo->rtImageRect;

	int nWidth, n, nGap=10;
	int nTop;
	CRect rtCoordi;
	CFont font, *pOldFont;
	LOGFONT logFont;
	logFont = g_FontSpeedmeter2;

	font.CreateFontIndirect(&logFont);
	pOldFont = dcBmpPanel.SelectObject(&font);

	dcBmpPanel.SetBkMode(TRANSPARENT);
	dcBmpPanel.SetTextColor(lpWndPos->dwColorLevel[1]);
	dcBmpPanel.TextOut(8, cy/2-10, m_sarrText.GetAt(GSen_title));

	dcBmpPanel.SelectObject(pOldFont);
	font.DeleteObject();

	logFont.lfHeight = 14;
	font.CreateFontIndirect(&logFont);
	pOldFont = dcBmpPanel.SelectObject(&font);

	dcBmpPanel.SetTextColor(lpWndPos->dwColorText);

	nWidth = (rtBmpPanel.Width()-60)/3;

	//////////////////////////////////////////////////////////////////////////
	//X
	LPST_BMP_INFO lpBmpInfoChk = theApp.BmpInfo.GetAt(biRadio_unchecked);
	rtCoordi.top = 1;
	rtCoordi.bottom = rtBmpPanel.Height()-1;
	nTop = rtDiv.top + (rtBmpPanel.Height()-(rtDiv.top+rtDiv.bottom))/2-8+1;
	for (n=0;n<GSEN_ANGLE;n++)
	{
		rtCoordi.right = rtBmpPanel.Width()-1-nWidth*n;
		rtCoordi.left = rtCoordi.right-nWidth+13;

		dcBmpPanel.BitBlt(rtCoordi.left, rtCoordi.top, rtDiv.left, rtDiv.top, &dcBmp, 0, 0, SRCCOPY);
		dcBmpPanel.StretchBlt(rtCoordi.left+rtDiv.left, rtCoordi.top, rtCoordi.Width()-(rtDiv.left+rtDiv.right), rtDiv.top,
			&dcBmp, rtDiv.left, rtImg.top, rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.top, SRCCOPY);
		dcBmpPanel.BitBlt(rtCoordi.right-rtDiv.right, rtCoordi.top, rtDiv.right, rtDiv.top,
			&dcBmp, rtImg.Width()-rtDiv.right, rtImg.top, SRCCOPY);

		dcBmpPanel.StretchBlt(rtCoordi.left, rtCoordi.top+rtDiv.top, rtDiv.left, rtCoordi.Height()-(rtDiv.top+rtDiv.bottom), 
			&dcBmp, 0, rtDiv.top, rtDiv.left, rtImg.Height()-(rtDiv.top+rtDiv.bottom), SRCCOPY);
		dcBmpPanel.StretchBlt(rtCoordi.left+rtDiv.left, rtCoordi.top+rtDiv.top,
			rtCoordi.Width()-(rtDiv.left+rtDiv.right), rtCoordi.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBmp, rtDiv.left, rtDiv.top,
			rtImg.Width()-(rtDiv.left+rtDiv.right), rtImg.Height()-(rtDiv.top+rtDiv.bottom), SRCCOPY);
		dcBmpPanel.StretchBlt(rtCoordi.right-rtDiv.right, rtCoordi.top+rtDiv.top, rtDiv.right, rtCoordi.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBmp, rtImg.Width()-rtDiv.right, rtDiv.top,
			rtDiv.right, rtImg.Height()-(rtDiv.top+rtDiv.bottom), SRCCOPY);

		dcBmpPanel.BitBlt(rtCoordi.left, rtCoordi.bottom-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
			&dcBmp, 0, rtImg.Height()-rtDiv.bottom, SRCCOPY);
		dcBmpPanel.StretchBlt(rtCoordi.left+rtDiv.left, rtCoordi.bottom-rtDiv.bottom, 
			rtCoordi.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
			&dcBmp, rtDiv.left, rtImg.Height()-rtDiv.bottom,
			rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom, SRCCOPY);
		dcBmpPanel.BitBlt(rtCoordi.right-rtDiv.right, rtCoordi.bottom-rtDiv.bottom, rtDiv.right, rtDiv.bottom,
			&dcBmp, rtImg.Width()-rtDiv.right, rtImg.Height()-rtDiv.bottom, SRCCOPY);

		switch (n)
		{
		case 0:		//Z
			dcBmpPanel.SetTextAlign(TA_LEFT);
			dcBmpPanel.TextOut(rtCoordi.left+22, nTop, m_sarrText.GetAt(GSen_Up));
			dcBmpPanel.SetTextAlign(TA_RIGHT);
			dcBmpPanel.TextOut(rtCoordi.right-22, nTop, m_sarrText.GetAt(GSen_Down));
			break;
		case 1:		//Y
			dcBmpPanel.SetTextAlign(TA_LEFT);
			dcBmpPanel.TextOut(rtCoordi.left+22, nTop, m_sarrText.GetAt(GSen_Left));
			dcBmpPanel.SetTextAlign(TA_RIGHT);
			dcBmpPanel.TextOut(rtCoordi.right-22, nTop, m_sarrText.GetAt(GSen_Right));
			break;
		case 2:		//X
			dcBmpPanel.SetTextAlign(TA_LEFT);
			dcBmpPanel.TextOut(rtCoordi.left+22, nTop, m_sarrText.GetAt(GSen_Front));
			dcBmpPanel.SetTextAlign(TA_RIGHT);
			dcBmpPanel.TextOut(rtCoordi.right-22, nTop, m_sarrText.GetAt(GSen_Rear));
			break;
		}

		m_rtCoordi[2-n] = rtCoordi;
		m_rtSenIndi[2-n][0] = CRect(rtCoordi.left+3, nTop, rtCoordi.left+3+lpBmpInfoChk->rtImageRect.Width(), nTop+lpBmpInfoChk->rtImageRect.Height());
		m_rtSenIndi[2-n][1] = CRect(rtCoordi.right-3-lpBmpInfoChk->rtImageRect.Width(), nTop, rtCoordi.right-3, nTop+lpBmpInfoChk->rtImageRect.Height());

	}
	//////////////////////////////////////////////////////////////////////////

	dcBmpPanel.SelectObject(pOldFont);
	font.DeleteObject();

	m_dcBmpPanel.BitBlt(lpWndPos->rtStickTo.left, lpWndPos->rtStickTo.top, rtBmpPanel.Width(), rtBmpPanel.Height(),
		&dcBmpPanel, 0, 0, SRCCOPY);

	//2013.04.29
	dcBmpPanel.SelectObject(pOldBmpPanel);
	bmpPanel.DeleteObject();					//(2) - de-select
	dcBmpPanel.DeleteDC();						//(1) - delete

	dcBmp.SelectObject(pOldBmp);
	dcBmp.DeleteDC();
	ReleaseDC(pDC);
}

void CWndGSensor::InitDraw_m1(int cx, int cy)
{
	CDC *pDC = GetDC();
	CDC dcBmp;
	CBitmap bmp, *pOldBmp;
	CPen *pOldPen;
	CBrush *pOldBrush;

	CDC dcBmpPanel;
	CBitmap bmpPanel, *pOldBmpPanel;
	CRect rtBmpPanel;
	CRect rect = CRect(0, 0, cx, cy);

	if (m_dcBmpPanel.m_hDC != NULL)
		m_dcBmpPanel.DeleteDC();

	if (m_bmpPanel.m_hObject != NULL)
		m_bmpPanel.DeleteObject();
	m_bmpPanel.CreateCompatibleBitmap(pDC, cx, cy);
	m_dcBmpPanel.CreateCompatibleDC(pDC);
	m_dcBmpPanel.SelectObject(&m_bmpPanel);

	LPST_WND_POS lpWndPos, lpWndPosCoordi;
	CRect rtDiv, rtImg;
	lpWndPos = theApp.WndPos.GetAt(wpGSensorView);
	lpWndPosCoordi = theApp.WndPos.GetAt(wpGSensorCoordi);
	rtDiv = lpWndPosCoordi->rtDiv;

	rtBmpPanel.left = 0;
	rtBmpPanel.right = cx-(lpWndPos->rtStickTo.left+lpWndPos->rtStickTo.right);
	rtBmpPanel.top = 0;
	rtBmpPanel.bottom = cy-(lpWndPos->rtStickTo.top+lpWndPos->rtStickTo.bottom);

	dcBmpPanel.CreateCompatibleDC(pDC);			//(1)
	bmpPanel.CreateCompatibleBitmap(pDC, rtBmpPanel.Width(), rtBmpPanel.Height());	//(2)
	pOldBmpPanel = dcBmpPanel.SelectObject(&bmpPanel);

	pOldPen = dcBmpPanel.SelectObject(&m_penEdge);
	pOldBrush = dcBmpPanel.SelectObject(&m_burshBoard);

	dcBmpPanel.Rectangle(&rtBmpPanel);

	dcBmpPanel.SelectObject(pOldBrush);
	dcBmpPanel.SelectObject(pOldPen);

	CBitmap *pBmp;
	LPST_BMP_INFO lpBmpInfo;

	lpBmpInfo = theApp.BmpInfo.GetAt(biGSenBG);
	if (lpBmpInfo->hbmpImage)
	{
		CDC dcBg;
		CBitmap *pBmpBg, *pOldBmpBg;

		dcBg.CreateCompatibleDC(pDC);
		pBmpBg = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
		pOldBmpBg = dcBg.SelectObject(pBmpBg);

		rtDiv = lpWndPos->rtDiv;
		rtImg = lpBmpInfo->rtImageRect;

		m_dcBmpPanel.BitBlt(0, 0, rtDiv.left, rtDiv.top, &dcBg, 0, 0, SRCCOPY);
		m_dcBmpPanel.StretchBlt(rtDiv.left, 0, rect.Width()-(rtDiv.left+rtDiv.right), rtDiv.top,
			&dcBg, rtDiv.left, 0, rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.top, SRCCOPY);
		m_dcBmpPanel.BitBlt(rect.Width()-rtDiv.right, 0, rtDiv.right, rtDiv.top,
			&dcBg, rtImg.Width()-rtDiv.right, 0, SRCCOPY);
		m_dcBmpPanel.StretchBlt(0, rtDiv.top, rtDiv.left, rect.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBg, 0, rtDiv.top, rtDiv.left, rtImg.Height()-(rtDiv.top+rtDiv.bottom), SRCCOPY);
		m_dcBmpPanel.StretchBlt(rect.Width()-rtDiv.right, rtDiv.top, rtDiv.right, rect.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBg, rtImg.Width()-rtDiv.right, rtDiv.top,
			rtDiv.right, rtImg.Height()-(rtDiv.top+rtDiv.bottom),
			SRCCOPY);
		m_dcBmpPanel.BitBlt(0, rect.Height()-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
			&dcBg, 0, rtImg.Height()-rtDiv.bottom, SRCCOPY);
		m_dcBmpPanel.StretchBlt(rtDiv.left, rect.Height()-rtDiv.bottom,
			rect.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
			&dcBg, rtDiv.left, rtImg.Height()-rtDiv.bottom,
			rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
			SRCCOPY);
		m_dcBmpPanel.BitBlt(rect.Width()-rtDiv.right, rect.Height()-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
			&dcBg, rtImg.Width()-rtDiv.right, rtImg.Height()-rtDiv.bottom, SRCCOPY);

		dcBg.SelectObject(pOldBmpBg);
		dcBg.DeleteDC();
	}


	lpBmpInfo = theApp.BmpInfo.GetAt(biGSenCoordi);
	pBmp = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
	dcBmp.CreateCompatibleDC(pDC);
	pOldBmp = dcBmp.SelectObject(pBmp);

	rtDiv = lpWndPosCoordi->rtDiv;
	rtImg = lpBmpInfo->rtImageRect;

	int nWidth, n, nGap=10;
	int nTop;
	CRect rtCoordi;
	CFont font, *pOldFont;
	LOGFONT logFont;
	logFont = g_FontSpeedmeter2;

	font.CreateFontIndirect(&logFont);
	pOldFont = dcBmpPanel.SelectObject(&font);

	dcBmpPanel.SetBkMode(TRANSPARENT);
	dcBmpPanel.SetTextColor(lpWndPos->dwColorLevel[1]);
	dcBmpPanel.TextOut(8, cy/2-10, m_sarrText.GetAt(GSen_title));

	dcBmpPanel.SelectObject(pOldFont);
	font.DeleteObject();

	logFont.lfHeight = 14;
	font.CreateFontIndirect(&logFont);
	pOldFont = dcBmpPanel.SelectObject(&font);

	dcBmpPanel.SetTextColor(lpWndPos->dwColorText);

	nWidth = (rtBmpPanel.Width()-60)/3;

	//////////////////////////////////////////////////////////////////////////
	//X
	LPST_BMP_INFO lpBmpInfoChk = theApp.BmpInfo.GetAt(biRadio_unchecked);
	rtCoordi.top = 1;
	rtCoordi.bottom = rtBmpPanel.Height()-1;
	nTop = rtDiv.top + (rtBmpPanel.Height()-(rtDiv.top+rtDiv.bottom))/2-8+1;
	for (n=0;n<GSEN_ANGLE;n++)
	{
		rtCoordi.right = rtBmpPanel.Width()-1-nWidth*n;
		rtCoordi.left = rtCoordi.right-nWidth+13;

		dcBmpPanel.BitBlt(rtCoordi.left, rtCoordi.top, rtDiv.left, rtDiv.top, &dcBmp, 0, 0, SRCCOPY);
		dcBmpPanel.StretchBlt(rtCoordi.left+rtDiv.left, rtCoordi.top, rtCoordi.Width()-(rtDiv.left+rtDiv.right), rtDiv.top,
			&dcBmp, rtDiv.left, rtImg.top, rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.top, SRCCOPY);
		dcBmpPanel.BitBlt(rtCoordi.right-rtDiv.right, rtCoordi.top, rtDiv.right, rtDiv.top,
			&dcBmp, rtImg.Width()-rtDiv.right, rtImg.top, SRCCOPY);

		dcBmpPanel.StretchBlt(rtCoordi.left, rtCoordi.top+rtDiv.top, rtDiv.left, rtCoordi.Height()-(rtDiv.top+rtDiv.bottom), 
			&dcBmp, 0, rtDiv.top, rtDiv.left, rtImg.Height()-(rtDiv.top+rtDiv.bottom), SRCCOPY);
		dcBmpPanel.StretchBlt(rtCoordi.left+rtDiv.left, rtCoordi.top+rtDiv.top,
			rtCoordi.Width()-(rtDiv.left+rtDiv.right), rtCoordi.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBmp, rtDiv.left, rtDiv.top,
			rtImg.Width()-(rtDiv.left+rtDiv.right), rtImg.Height()-(rtDiv.top+rtDiv.bottom), SRCCOPY);
		dcBmpPanel.StretchBlt(rtCoordi.right-rtDiv.right, rtCoordi.top+rtDiv.top, rtDiv.right, rtCoordi.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBmp, rtImg.Width()-rtDiv.right, rtDiv.top,
			rtDiv.right, rtImg.Height()-(rtDiv.top+rtDiv.bottom), SRCCOPY);

		dcBmpPanel.BitBlt(rtCoordi.left, rtCoordi.bottom-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
			&dcBmp, 0, rtImg.Height()-rtDiv.bottom, SRCCOPY);
		dcBmpPanel.StretchBlt(rtCoordi.left+rtDiv.left, rtCoordi.bottom-rtDiv.bottom, 
			rtCoordi.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
			&dcBmp, rtDiv.left, rtImg.Height()-rtDiv.bottom,
			rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom, SRCCOPY);
		dcBmpPanel.BitBlt(rtCoordi.right-rtDiv.right, rtCoordi.bottom-rtDiv.bottom, rtDiv.right, rtDiv.bottom,
			&dcBmp, rtImg.Width()-rtDiv.right, rtImg.Height()-rtDiv.bottom, SRCCOPY);

		/*
		switch (n)
		{
		case 0:		//Z
			dcBmpPanel.SetTextAlign(TA_LEFT);
			dcBmpPanel.TextOut(rtCoordi.left+22, nTop, m_sarrText.GetAt(GSen_Up));
			dcBmpPanel.SetTextAlign(TA_RIGHT);
			dcBmpPanel.TextOut(rtCoordi.right-22, nTop, m_sarrText.GetAt(GSen_Down));
			break;
		case 1:		//Y
			dcBmpPanel.SetTextAlign(TA_LEFT);
			dcBmpPanel.TextOut(rtCoordi.left+22, nTop, m_sarrText.GetAt(GSen_Left));
			dcBmpPanel.SetTextAlign(TA_RIGHT);
			dcBmpPanel.TextOut(rtCoordi.right-22, nTop, m_sarrText.GetAt(GSen_Right));
			break;
		case 2:		//X
			dcBmpPanel.SetTextAlign(TA_LEFT);
			dcBmpPanel.TextOut(rtCoordi.left+22, nTop, m_sarrText.GetAt(GSen_Front));
			dcBmpPanel.SetTextAlign(TA_RIGHT);
			dcBmpPanel.TextOut(rtCoordi.right-22, nTop, m_sarrText.GetAt(GSen_Rear));
			break;
		}
		*/

		m_rtCoordi[2-n] = rtCoordi;
		m_rtSenIndi[2-n][0] = CRect(rtCoordi.left+3, nTop, rtCoordi.left+3+lpBmpInfoChk->rtImageRect.Width(), nTop+lpBmpInfoChk->rtImageRect.Height());
		m_rtSenIndi[2-n][1] = CRect(rtCoordi.right-3-lpBmpInfoChk->rtImageRect.Width(), nTop, rtCoordi.right-3, nTop+lpBmpInfoChk->rtImageRect.Height());

	}
	//////////////////////////////////////////////////////////////////////////

	dcBmpPanel.SelectObject(pOldFont);
	font.DeleteObject();

	m_dcBmpPanel.BitBlt(lpWndPos->rtStickTo.left, lpWndPos->rtStickTo.top, rtBmpPanel.Width(), rtBmpPanel.Height(),
		&dcBmpPanel, 0, 0, SRCCOPY);

	//2013.04.19
	dcBmpPanel.SelectObject(pOldBmpPanel);
	bmpPanel.DeleteObject();
	dcBmpPanel.DeleteDC();

	dcBmp.SelectObject(pOldBmp);
	dcBmp.DeleteDC();
	ReleaseDC(pDC);
}

void CWndGSensor::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	if ((cx==0)||(cy==0))
		return;

	InitDraw(cx, cy);
}

void CWndGSensor::SetSensorValue(float fX, float fY, float fZ)
{
	float fGap;
	fGap = fX - m_fSensorX;

	if (fGap < - ALARM_X)
	{
		m_dwSetTime2[0][0] = (DWORD)time(0) + 30;
		m_bSensorOn[0][0] = TRUE;
	}
	else if (fGap > ALARM_X)
	{
		m_dwSetTime2[0][1] = (DWORD)time(0) + 30;
		m_bSensorOn[0][1] = TRUE;
	}

	fGap = fY - m_fSensorY;

	if (fGap < - ALARM_Y)
	{
		m_dwSetTime2[1][0] = (DWORD)time(0) + 30;
		m_bSensorOn[1][0] = TRUE;
	}
	else if (fGap > ALARM_Y)
	{
		m_dwSetTime2[1][1] = (DWORD)time(0) + 30;
		m_bSensorOn[1][1] = TRUE;
	}

	fGap = fZ - m_fSensorZ;

	if (fGap < - ALARM_Z)
	{
		m_dwSetTime2[2][0] = (DWORD)time(0) + 30;
		m_bSensorOn[2][0] = TRUE;
	}
	else if (fGap > ALARM_Z)
	{
		m_dwSetTime2[2][1] = (DWORD)time(0) + 30;
		m_bSensorOn[2][1] = TRUE;
	}

	int n, i;

	for (n=0;n<GSEN_ANGLE;n++)
	{
		for (i=0;i<GSEN_DIRECTION;i++)
		{
			if (m_dwSetTime2[n][i]>time(0))
				m_dwSetTime2[n][i]--;
			else if (m_dwSetTime2[n][i]<=time(0))
			{
				m_bSensorOn[n][i] = FALSE;
				m_dwSetTime2[n][i] = 0;
			}
		}
	}


	m_fSensorX	= fX;
	m_fSensorY  = fY;
	m_fSensorZ	= fZ;

	UpdateGSensor();
}

void CWndGSensor::UpdateGSensor()
{
	LPST_WND_POS lpWndPos = theApp.WndPos.GetAt(wpGSensorView);
	if (lpWndPos->nMode == 0)
		UpdateGSensor_m0();
	else
		UpdateGSensor_m1();
}

void CWndGSensor::UpdateGSensor_m0()
{
	CDC *pDC = GetDC();
	CRect rect, rtCoordi;
	this->GetClientRect(&rect);

	CDC dcMemory;
	CDC dcBmpOn, dcBmpOff;
	CBitmap bmp, *pOldBmp;
	CBitmap *pOldBmpOn, *pOldBmpOff;

	dcBmpOn.CreateCompatibleDC(pDC);
	dcBmpOff.CreateCompatibleDC(pDC);
	LPST_BMP_INFO lpBmpInfoOn, lpBmpInfoOff;
	CBitmap *pBmpOn, *pBmpOff;
	lpBmpInfoOff = theApp.BmpInfo.GetAt(biRadio_unchecked);
	pBmpOff = CBitmap::FromHandle(lpBmpInfoOff->hbmpImage);
	pOldBmpOff = dcBmpOff.SelectObject(pBmpOff);
	lpBmpInfoOn = theApp.BmpInfo.GetAt(biRadio_checked);
	pBmpOn = CBitmap::FromHandle(lpBmpInfoOn->hbmpImage);
	pOldBmpOn = dcBmpOn.SelectObject(pBmpOn);

	dcMemory.CreateCompatibleDC(pDC);
	bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
	pOldBmp = dcMemory.SelectObject(&bmp);

	dcMemory.BitBlt(0, 0, rect.Width(), rect.Height(), &m_dcBmpPanel, 0, 0, SRCCOPY);


	dcMemory.SetBkMode(TRANSPARENT);

	LPST_WND_POS lpWndPos;
	int i, n, nWidth;
	CString strGSen[GSEN_ANGLE];
	COLORREF crRGB[GSEN_ANGLE];
	nWidth = (rect.Width()-60)/3;

	lpWndPos = theApp.WndPos.GetAt(wpGSensorView);

	strGSen[0].Format(_T("X: %.4f"), m_fSensorX);
	strGSen[1].Format(_T("Y: %.4f"), m_fSensorY);
	strGSen[2].Format(_T("Z: %.4f"), m_fSensorZ);

	crRGB[0] = lpWndPos->dwColorLevel[2];
	crRGB[1] = lpWndPos->dwColorLevel[3];
	crRGB[2] = lpWndPos->dwColorLevel[4];

	for (n=0;n<GSEN_ANGLE;n++)
	{
		dcMemory.SetTextAlign(TA_CENTER);
		dcMemory.SetTextColor(crRGB[n]);
		dcMemory.TextOut(m_rtCoordi[n].left +m_rtCoordi[n].Width()/2+lpWndPos->rtStickTo.left, 
						 m_rtCoordi[n].top+2+lpWndPos->rtStickTo.top, strGSen[n]);

		for (i=0;i<GSEN_DIRECTION;i++)
		{
			if (m_bSensorOn[n][i])
				dcMemory.BitBlt(m_rtSenIndi[n][i].left+lpWndPos->rtStickTo.left, m_rtSenIndi[n][i].top+lpWndPos->rtStickTo.top,
								m_rtSenIndi[n][i].Width(), m_rtSenIndi[n][i].Height(),
								&dcBmpOn, 0, 0, SRCCOPY);
			else
				dcMemory.BitBlt(m_rtSenIndi[n][i].left+lpWndPos->rtStickTo.left, m_rtSenIndi[n][i].top+lpWndPos->rtStickTo.top,
								m_rtSenIndi[n][i].Width(), m_rtSenIndi[n][i].Height(),
								&dcBmpOff, 0, 0, SRCCOPY);

				
		}
	};
	

	pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dcMemory, 0, 0, SRCCOPY);

	dcBmpOn.SelectObject(pOldBmpOn);
	dcBmpOff.SelectObject(pOldBmpOff);
	dcMemory.SelectObject(pOldBmp);
	dcMemory.DeleteDC();
	dcBmpOn.DeleteDC();
	dcBmpOff.DeleteDC();
	bmp.DeleteObject();
	ReleaseDC(pDC);

}

void CWndGSensor::UpdateGSensor_m1()
{
	CDC *pDC = GetDC();
	CRect rect, rtCoordi;
	this->GetClientRect(&rect);

	CDC dcMemory;
	CDC dcBmpOn, dcBmpOff;
	CBitmap bmp, *pOldBmp;
	CBitmap *pOldBmpOn, *pOldBmpOff;

	dcBmpOn.CreateCompatibleDC(pDC);
	dcBmpOff.CreateCompatibleDC(pDC);
	LPST_BMP_INFO lpBmpInfoOn, lpBmpInfoOff;
	CBitmap *pBmpOn, *pBmpOff;
	lpBmpInfoOff = theApp.BmpInfo.GetAt(biRadio_unchecked);
	pBmpOff = CBitmap::FromHandle(lpBmpInfoOff->hbmpImage);
	pOldBmpOff = dcBmpOff.SelectObject(pBmpOff);
	lpBmpInfoOn = theApp.BmpInfo.GetAt(biRadio_checked);
	pBmpOn = CBitmap::FromHandle(lpBmpInfoOn->hbmpImage);
	pOldBmpOn = dcBmpOn.SelectObject(pBmpOn);

	dcMemory.CreateCompatibleDC(pDC);
	bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
	pOldBmp = dcMemory.SelectObject(&bmp);

	dcMemory.BitBlt(0, 0, rect.Width(), rect.Height(), &m_dcBmpPanel, 0, 0, SRCCOPY);


	dcMemory.SetBkMode(TRANSPARENT);

	LPST_WND_POS lpWndPos;
	int n, nWidth;
	CString strGSen[GSEN_ANGLE];
	COLORREF crRGB[GSEN_ANGLE];
	nWidth = (rect.Width()-60)/3;

	lpWndPos = theApp.WndPos.GetAt(wpGSensorView);

	strGSen[0].Format(_T("X: %.4f"), m_fSensorX);
	strGSen[1].Format(_T("Y: %.4f"), m_fSensorY);
	strGSen[2].Format(_T("Z: %.4f"), m_fSensorZ);

	crRGB[0] = lpWndPos->dwColorLevel[2];
	crRGB[1] = lpWndPos->dwColorLevel[3];
	crRGB[2] = lpWndPos->dwColorLevel[4];

	for (n=0;n<GSEN_ANGLE;n++)
	{
		dcMemory.SetTextAlign(TA_CENTER);
		dcMemory.SetTextColor(crRGB[n]);
		dcMemory.TextOut(m_rtCoordi[n].left +m_rtCoordi[n].Width()/2+lpWndPos->rtStickTo.left, 
			m_rtCoordi[n].top+3+lpWndPos->rtStickTo.top, strGSen[n]);

		/*
		for (i=0;i<GSEN_DIRECTION;i++)
		{
			if (m_bSensorOn[n][i])
				dcMemory.BitBlt(m_rtSenIndi[n][i].left+lpWndPos->rtStickTo.left, m_rtSenIndi[n][i].top+lpWndPos->rtStickTo.top,
				m_rtSenIndi[n][i].Width(), m_rtSenIndi[n][i].Height(),
				&dcBmpOn, 0, 0, SRCCOPY);
			else
				dcMemory.BitBlt(m_rtSenIndi[n][i].left+lpWndPos->rtStickTo.left, m_rtSenIndi[n][i].top+lpWndPos->rtStickTo.top,
				m_rtSenIndi[n][i].Width(), m_rtSenIndi[n][i].Height(),
				&dcBmpOff, 0, 0, SRCCOPY);


		}
		*/
	};


	pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dcMemory, 0, 0, SRCCOPY);

	dcBmpOn.SelectObject(pOldBmpOn);
	dcBmpOff.SelectObject(pOldBmpOff);
	dcMemory.SelectObject(pOldBmp);
	dcMemory.DeleteDC();
	dcBmpOn.DeleteDC();
	dcBmpOff.DeleteDC();
	bmp.DeleteObject();
	ReleaseDC(pDC);

}

void CWndGSensor::RefreshLang()
{
	LoadText();

	CRect rect;
	this->GetClientRect(&rect);
	InitDraw(rect.Width(), rect.Height());

	UpdateGSensor();
}

void CWndGSensor::LoadText()
{
	m_sarrText.RemoveAll();
	m_sarrText.Add(
		theApp.GetUIString(APPNAME_GSENSOR, KEYNAME_GSENSOR_TITLE, _T("G SENSOR")));


	m_sarrText.Add(theApp.GetUIString(APPNAME_GSENSOR, KEYNAME_GSENSOR_FRONT, KEYNAME_GSENSOR_FRONT));
	m_sarrText.Add(theApp.GetUIString(APPNAME_GSENSOR, KEYNAME_GSENSOR_REAR, KEYNAME_GSENSOR_REAR));
	m_sarrText.Add(theApp.GetUIString(APPNAME_GSENSOR, KEYNAME_GSENSOR_LEFT, KEYNAME_GSENSOR_LEFT));
	m_sarrText.Add(theApp.GetUIString(APPNAME_GSENSOR, KEYNAME_GSENSOR_RIGHT, KEYNAME_GSENSOR_RIGHT));
	m_sarrText.Add(theApp.GetUIString(APPNAME_GSENSOR, KEYNAME_GSENSOR_UP, KEYNAME_GSENSOR_UP));
	m_sarrText.Add(theApp.GetUIString(APPNAME_GSENSOR, KEYNAME_GSENSOR_DOWN, KEYNAME_GSENSOR_DOWN));
}